<template>
  <div>
    <div class="crumbs">
      <el-breadcrumb separator="/">
        <el-breadcrumb-item>
          <i class="el-icon-lx-cascades"></i> 基础表格
        </el-breadcrumb-item>
      </el-breadcrumb>
    </div>
    <div class="container">
      <div class="handle-box">
        <div class="block" style="float: left; margin-right: 10px;">
          <el-date-picker
            v-model="value2"
            type="daterange"
            align="left"
            unlink-panels
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
          >
          </el-date-picker>
        </div>

        <el-button type="primary" @click="handleSearch">查询</el-button>
        <el-button type="primary" @click="handleExport">导出</el-button>
      </div>
      <el-table
        :data="tableData"
        border
        class="table"
        header-cell-class-name="table-header"
      >
        <el-table-column
          prop="productText"
          align="center"
          label="品类名称"
        ></el-table-column>
        <el-table-column
          prop="weightGross"
          align="center"
          label="毛重(kg)"
        ></el-table-column>
        <el-table-column
          prop="weightTare"
          align="center"
          label="皮重(kg)"
        ></el-table-column>
        <el-table-column
          prop="weightNet"
          align="center"
          label="净重(kg)"
        ></el-table-column>
      </el-table>
    </div>
  </div>
</template>

<script>
import NProgress from "nprogress";
import { createLogger } from "vuex";
export default {
  name: "BangningSortCenter",
  data() {
    return {
      value2: "",
      query: {
        address: "",
        name: "",
        pageIndex: 1,
        pageSize: 10,
      },
      tableData: [],
      multipleSelection: [],
      delList: [],
      editVisible: false,
      pageTotal: 0,
      form: {},
      idx: -1,
      id: -1,
    };
  },

  //   created() {
  //     // this.getData();
  //   },

  // 使用keep-alive包裹后触发
  activated() {
    console.log(222);
    this.getData();
  },
  methods: {
    getData() {
      this.$axios
        .get("/papercollection/count", {
          params: {
            startDate: this.value2 ? this.value2[0].getTime() : "",
            endDate: this.value2
              ? (
                  this.value2[1].getTime() +
                  (24 * 3600 * 1000 - 1000)
                ).toString()
              : "",
          },
        })
        .then(({ data: res }) => {
          console.log(res);
          if (res.code != 0) {
            return this.$message.warning(res.msg);
          }
          this.tableData = res.data;
        })
        .catch((err) => {});
    },
    // 触发搜索按钮
    handleSearch() {
      if (this.value2 == "") {
        return this.$message.warning({
          message: "请选择查询时间段",
          duration: 1500,
        });
      }
      this.$set(this.query, "pageIndex", 1);
      this.getData();
    },
    handleExport() {
      if (this.value2 == "") {
        return this.$message.warning({
          message: "请选择导出时间段",
          duration: 1500,
        });
      }
      NProgress.start();
      this.$axios
        .get("/papercollection/countExport", {
          params: {
            startDate: this.value2 ? this.value2[0].getTime().toString() : "",
            endDate: this.value2
              ? (
                  this.value2[1].getTime() +
                  (24 * 3600 * 1000 - 1000)
                ).toString()
              : "",
          },
          responseType: "blob",
        })
        .then((res) => {
          console.log(res);
          if (res.data.type != "application/vnd.ms-excel") {
            NProgress.done();
            return this.$message.warning({
              message: res.msg,
              duration: 1500,
            });
          }

          // 导出excel表格
          const link = document.createElement("a");
          let blob = new Blob([res.data], { type: "application/vnd.ms-excel" });
          link.style.display = "none";
          link.href = URL.createObjectURL(blob);
          //   let num = "";
          //   for (let i = 0; i < 10; i++) {
          //     num += Math.ceil(Math.random() * 10);
          //   }
          //   link.setAttribute("download", "用户_" + num + ".xls");

          link.setAttribute("download", "邦凝分拣中心统计" + ".xls");
          document.body.appendChild(link);
          link.click();
          document.body.removeChild(link);

          NProgress.done();
        })
        .catch((err) => {});
    },
  },
};
</script>

<style scoped>
.handle-box {
  margin-bottom: 20px;
}

.handle-select {
  width: 120px;
}

.handle-input {
  width: 300px;
  display: inline-block;
}
.table {
  width: 100%;
  font-size: 14px;
}
.red {
  color: #ff0000;
}
.mr10 {
  margin-right: 10px;
}
.table-td-thumb {
  display: block;
  margin: auto;
  width: 40px;
  height: 40px;
}
</style>
